package com.artwork.controller;

import com.artwork.common.enums.JsonResultEnum;
import com.artwork.common.exception.JsonException;
import com.artwork.model.*;
import com.artwork.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.http.HTTPException;
import java.util.List;

/**
 * @Author JoJo
 * @Data 2021/9/8$ 13:43$
 * @Description
 * @Param $
 * @Return $
 */
@Controller
@RequestMapping("/admin")
public class IndexController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    UserService userService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    ArticleService articleService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    TicketService ticketService;
    @Autowired
    OrderService orderService;

    @GetMapping
    public String index(){
        return "index";
    }

    /**
     * desc 登录
     * @param name
     * @param password
     * @param request
     * @return
     * @throws JsonException
     */
    @PostMapping("/login")
    public String login(String name, String password, HttpServletRequest request) throws JsonException{
        SysUser user = sysUserService.loginAction(name,password);
        if (user == null) {
            throw new JsonException(JsonResultEnum.ADMIN_USER_NULL);
        }
        HttpSession session = request.getSession();
        session.setAttribute("adminId", user.getId());
        session.setAttribute("adminName", user.getUsername());
        session.setAttribute("admin", user);
        return "index/home";
    }

    /**
     * desc 用户列表
     * @param page
     * @param request
     * @return
     * @throws HTTPException
     */
    @GetMapping("/user")
    public String getList(Page page, HttpServletRequest request) throws HTTPException{
        List<User> userList = userService.getList();
        PageHelper.startPage(page.getPage(), page.getRows());
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        request.setAttribute("userList", userList);
        request.setAttribute("pageInfo", pageInfo);
        return "index/user";
    }

    /**
     * desc 公告列表
     * @param page
     * @param request
     * @return
     * @throws HTTPException
     */
    @GetMapping("/notice")
    public String getNoticeList(Page page, HttpServletRequest request) throws HTTPException{
        List<Notice> notices = noticeService.getList();
        PageHelper.startPage(page.getPage(), page.getRows());
        PageInfo<Notice> pageInfo = new PageInfo<>(notices);
        request.setAttribute("noticeList", notices);
        request.setAttribute("pageInfo", pageInfo);
        return "article/notice";
    }

    /**
     * desc 文章列表
     * @param page
     * @param request
     * @return
     * @throws HTTPException
     */
    @GetMapping("/article")
    public String getArticleList(Page page, HttpServletRequest request) throws HTTPException{
        List<Article> articles = articleService.getList();
        PageHelper.startPage(page.getPage(), page.getRows());
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        request.setAttribute("noticeList", articles);
        request.setAttribute("pageInfo", pageInfo);
        return "article/notice";
    }

    /**
     * desc 商品列表
     * @param page
     * @param type 0转拍 1兑换
     * @param request
     * @return
     */
    @GetMapping("/goods")
    public String getGoodsList(Page page,int type, HttpServletRequest request) throws HTTPException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<Goods> goodsList = goodsService.getListByType(type);
        PageInfo<Goods> pageInfo = new PageInfo(goodsList);
        request.setAttribute("goodsList", goodsList);
        request.setAttribute("pageInfo", pageInfo);
        return "goods/goods";
    }

    /**
     * desc 代金券列表
     * @param page
     * @param request
     * @return
     */
    @GetMapping("/ticket")
    public String getTicketList(Page page, HttpServletRequest request) throws HTTPException{
        List<Ticket> tickets = ticketService.getList();
        PageHelper.startPage(page.getPage(), page.getRows());
        PageInfo<Ticket> pageInfo = new PageInfo<>(tickets);
        request.setAttribute("noticeList", tickets);
        request.setAttribute("pageInfo", pageInfo);
        return "goods/ticket";
    }

    /**
     * @desc 订单列表
     * @param page
     * @param request
     * @return
     * @throws HTTPException
     */
    @GetMapping("/order")
    public String getOrderList(Page page, HttpServletRequest request) throws HTTPException{
        List<Order> orders = orderService.getList();
        PageHelper.startPage(page.getPage(), page.getRows());
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        request.setAttribute("noticeList", orders);
        request.setAttribute("pageInfo", pageInfo);
        return "goods/ticket";
    }
}
